package com.newlink.building.userinfo.activity

import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.newlink.building.common_base.base.NL_BaseActivity
import com.newlink.building.common_base.utils.DialogUtil
import com.newlink.building.common_base.utils.LogHandlerThread
import com.newlink.building.common_base.widget.CustomDialog
import com.newlink.building.userinfo.R
import com.newlink.building.userinfo.databinding.ActivityLogBinding
import org.linphone.core.tools.Log

/**
 * Created by xiaodong on 2024/10/12.
 */
class Module_UserInfo_LogActivityNL : NL_BaseActivity() {
    private lateinit var mBinding: ActivityLogBinding
    private val logHandlerThread: LogHandlerThread? = LogHandlerThread.instance
    override fun attachLayoutRes(): View {
        mBinding = ActivityLogBinding.inflate(layoutInflater)
        return mBinding.root
    }

    private val mDialog by lazy {
        DialogUtil.getWaitDialog(this)
    }

    override fun initData() {
        mDialog.show()
        logHandlerThread?.readLog(this, object : LogHandlerThread.LogReadCallback {
            override fun onFileRead(content: String?) {
                runOnUiThread {
                    mDialog.dismiss()
                    mBinding.tvContent.text = content
//                    mBinding.tvLog.movementMethod = ScrollingMovementMethod.getInstance()
                    mBinding.scrollView.post {
                        mBinding.scrollView.smoothScrollTo(0, mBinding.tvContent.bottom)
//                        mBinding.scrollView.scrollTo(0, mBinding.scrollView.getChildAt(0).bottom)

                    }
                }
            }
        })
    }

    override fun initView() {
        val tvTitleBarLeft = findViewById<TextView>(R.id.tv_title_bar_back)
        val tvTitleBarCenter = findViewById<TextView>(R.id.tv_title_bar_center)
        val ivTitleBarRight = findViewById<ImageView>(R.id.iv_title_bar_right)
        tvTitleBarCenter.text = "调试日志"
        ivTitleBarRight.visibility = View.VISIBLE
        tvTitleBarLeft.visibility = View.VISIBLE
        ivTitleBarRight.setBackgroundResource(R.mipmap.title_bar_delete)

        ivTitleBarRight.setOnClickListener {
            CustomDialog(this).setMessage("确定删除日志吗?")
                .setPositiveButton(object : CustomDialog.OnClickListener {
                    override fun onClick(dialog: CustomDialog) {
                        logHandlerThread?.clearLog(this@Module_UserInfo_LogActivityNL)
                        mBinding.tvContent.text = ""
                        tvTitleBarLeft.text = "0 KB"
                        dialog.dismiss()
                    }
                }).setNegativeButton(object : CustomDialog.OnClickListener {
                    override fun onClick(dialog: CustomDialog) {
                        dialog.dismiss()
                    }
                }).show()
        }

//        mBinding.tvLog.movementMethod = ScrollingMovementMethod.getInstance()

        val logFileSize = logHandlerThread?.getLogFileMegaSize(this)
        Log.e("[LogActivity]  logFileSize $logFileSize")
        if (logFileSize != null) {
            if (logFileSize < 0.1) {
                tvTitleBarLeft.text = "${(logFileSize * 1000).toInt()} KB"
            } else {
                tvTitleBarLeft.text = "${String.format("%.3f", logFileSize)} MB"
            }
        }
    }
}